package com.qixiaobao.nondirect.settle.service.impl;

import com.qixiaobao.common.core.domain.model.LoginUser;
import com.qixiaobao.common.exception.UtilException;
import com.qixiaobao.common.utils.SecurityUtils;
import com.qixiaobao.common.utils.StringUtils;
import com.qixiaobao.nondirect.settle.domain.SupplyPremiumPaid;
import com.qixiaobao.nondirect.settle.domain.dto.SupplyPremiumPaidDTO;
import com.qixiaobao.nondirect.settle.mapper.SupplyPremiumPaidMapper;
import com.qixiaobao.nondirect.settle.service.ISupplyPremiumPaidService;
import com.qixiaobao.system.domain.Dict;
import com.qixiaobao.system.mapper.DictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 已付保费 业务层处理
 * @Author yangxm
 * @Date 2025/7/1 17:13
 */
@Service
public class SupplyPremiumPaidServiceImpl implements ISupplyPremiumPaidService {

    @Autowired
    private SupplyPremiumPaidMapper supplierPremiumPaidMapper;
    @Autowired
    private DictMapper dictMapper;

    /**
     * @Description 查询 已付保费列表
     * @Author yangxm
     * @Date 2025/7/1 17:15
     * @param supplyPremiumPaidDTO
     * @return java.util.List<com.qixiaobao.nondirect.domain.SupplyPremiumPaid>
     */
    @Override
    public List<SupplyPremiumPaid> selectSupplyPremiumPaidList(SupplyPremiumPaidDTO supplyPremiumPaidDTO) {
        List<SupplyPremiumPaid> list = supplierPremiumPaidMapper.selectSupplyPremiumPaidList(supplyPremiumPaidDTO);
        if (StringUtils.isNotEmpty(list)){
            Map<String, String> insureFeePayTypeMap = dictMapper.selectDictListByType("insure_fee_pay_type").stream().collect(Collectors.toMap(Dict::getK, Dict::getVal));
            SupplyPremiumPaid supplyPremiumPaid;
            for (int i = 0; i < list.size(); i++) {
                supplyPremiumPaid = list.get(i);
                supplyPremiumPaid.setPayFlagVal("1".equals(supplyPremiumPaid.getPayFlag()) ? "正常" : "撤销");
                supplyPremiumPaid.setPayTypeVal("1".equals(supplyPremiumPaid.getPayType()) ? "公户转账" : "2".equals(supplyPremiumPaid.getPayType()) ? "个户转账" : "");
                supplyPremiumPaid.setInsureFeePayTypeVal(insureFeePayTypeMap.getOrDefault(supplyPremiumPaid.getInsureFeePayType(), ""));
            }
        }
        return list;
    }

    /**
     * @Description 已付保费 - 支付金额总计
     * @Author yangxm
     * @Date 2025/7/1 17:33
     * @param supplyPremiumPaidDTO
     * @return java.math.BigDecimal
     */
    @Override
    public BigDecimal selectSupplyPremiumPaidTotal(SupplyPremiumPaidDTO supplyPremiumPaidDTO) {
        List<SupplyPremiumPaid> list = supplierPremiumPaidMapper.selectSupplyPremiumPaidList(supplyPremiumPaidDTO);
        if (StringUtils.isNotEmpty(list)){
            return list.stream().map(SupplyPremiumPaid::getActualPremium).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
        }
        return BigDecimal.ZERO;
    }

    /**
     * @Description 根据id，查询已付保费信息及对应的银行回单明细(主子表结构)
     * @Author yangxm
     * @Date 2025/7/2 8:55
     * @param id
     * @return com.qixiaobao.nondirect.domain.SupplyPremiumPaid
     */
    @Override
    public SupplyPremiumPaid selectInsureFeeReceiptById(Long id) {
        return supplierPremiumPaidMapper.selectInsureFeeReceiptById(id);
    }

    /**
     * @Description 修改付款备注
     * @Author yangxm
     * @Date 2025/7/2 9:00
     * @param supplyPremiumPaid
     * @return int
     */
    @Override
    public int updatePayRemark(SupplyPremiumPaid supplyPremiumPaid) {
        LoginUser user = SecurityUtils.getLoginUser();
        if (user == null){
            throw new UtilException("登陆超时，请重新登陆");
        }
        supplyPremiumPaid.setPayRemarkOperator(user.getUsername());
        return supplierPremiumPaidMapper.updatePayRemark(supplyPremiumPaid);
    }
}
